package day14;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server=new ServerSocket(9999);
        while (true) {
            Socket client = server.accept();//等待客户端连接
            //通过线程 模拟每个客户端连接之后的读写逻辑
            new ServerThread(client).start();
        }
    }
}
//只负责接收每个客户端 写入多次数据
class ServerThread extends Thread{
    Socket client;
    public ServerThread(Socket client){
        this.client=client;
    }
    public void run() {
        try {
            BufferedReader br=new BufferedReader(
                    new InputStreamReader( client.getInputStream()));//字节转字符
            while (true){
                String msg=br.readLine();
                System.out.println(client.getInetAddress()+":"+msg);//ip地址
            }
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
}